﻿using Castle.Core;
using Castle.MicroKernel;
using Abp.Application.Services;
using Abp.Dependency;

namespace Abp.Application.Transactions
{
    /// <summary>
    /// 事物拦截器注册
    /// </summary>
    public static class TransactionInterceptorRegistrar
    {
        public static void Initialize()
        {
            ObjectContainer.RegisterType(typeof(TransactionInterceptor));
            ((CastleObjectContainer)ObjectContainer.Current).Container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
        }

        private static void Kernel_ComponentRegistered(string key, IHandler handler)
        {
            if (typeof(IServiceManager).IsAssignableFrom(handler.ComponentModel.Implementation))
            {
                handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(TransactionInterceptor)));
            }
        }
    }
}